********************************************************************************
** 	TITLE: f6_balance_postignite_wpred.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table VIII: Court Delay Balance Tests and First Stage – Post-IGNITE

********************************************************************************
		set sortseed 13
use "$output_data/prediction.dta", clear

//Use the normal definition for stay
gen stay = stay_rec

/*********************************************************************************
Post-IGNITE
**********************************************************************************/	
preserve
keep if D_ignite_proxy == 1 & e_sample == 1
	 	est clear
		local zipvar D_missing  pctebll  pc_hs log_med_hhincome pc_black
		
						reghdfe D_remove_any $auxiliary_control age_65plus ///
							, cluster(inmate) a($design_control)
						eststo bal_joint1
						testparm  $auxiliary_control age_65plus
						local fp = r(p)
						local fp: di %8.3f `fp'
						local fp = subinstr("`fp'"," ","",.)
						
						local fs = r(F)
						local fs: di %8.3f `fs'
						local fs = subinstr("`fs'"," ","",.)
						local fs = " \multicolumn{3}{c}{`fs' [`fp']}"
						estadd local Fs "`fs'": bal_joint1
					
					local Num = e(N)
					local Num : display %8s `Num'
					local clust = e(N_clust)
					local clust : display %8s `clust'
					
					local Num = "&\"+"multicolumn{2}{c}{`Num'}"
					local clust = "&\"+"multicolumn{2}{c}{`clust'}"
					local cont = "&\"+"multicolumn{2}{c}{Yes}"
					
					
					estadd local Num "`Num'" : bal_joint1
					estadd local clust "`clust'" : bal_joint1
					estadd local cont "`cont'": bal_joint1
					
				foreach v of varlist stay D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black prior_offense_1y D_public_defender return_3m_hat {
					
					reghdfe `v' D_remove_any if `v'>=0, cluster(inmate) a($design_control)
					local `v'_b =_b[D_remove_any]
					local `v'_se = _se[D_remove_any]
					
					local `v'_b: display %8.3f ``v'_b'
					local `v'_se: display %8.3f ``v'_se'
					
					mat test = r(table)
					local p_val =  el(test,4,1)
						if `p_val'<0.01 {
							local star1="***"
						}
						if `p_val'>=0.01 & `p_val'<0.05 {
							local star1="**"
						}
						if `p_val'>=0.05 & `p_val'<0.1 {
							local star1="*"
						}
						if `p_val'>=0.1 {
							local star1=""
						}
					
					local `v'_b = "``v'_b'"+"`star1'"
					local `v'_se = subinstr("(``v'_se')"," ","",.)
						
					estadd local `v'_b "``v'_b'" : bal_joint1
												
						su `v' if e(sample)==1
						
						local `v'_mean = r(mean)
						local `v'_sd = r(sd)
						local `v'_mean: display %8.3f ``v'_mean'
						local `v'_sd: display %8.3f ``v'_sd'
						local `v'_sd = subinstr("(``v'_sd')"," ","",.)
						
						local `v'_se = "&"+ "``v'_se'"
						local `v'_diff = "``v'_mean'" + "&" + "``v'_b'" + "``v'_se'"
						
						estadd local `v'_diff "``v'_diff'" : bal_joint1
						estadd local `v'_se "``v'_se'" : bal_joint1
				}
				
					foreach v in `zipvar' {
						reghdfe `v' D_remove_any , cluster(inmate) a($design_control)
						local `v'_b =_b[D_remove_any]
						local `v'_se = _se[D_remove_any]
						
						local `v'_b: display %8.3f ``v'_b'
						local `v'_se: display %8.3f ``v'_se'
						
						mat test = r(table)
						local p_val =  el(test,4,1)
							if `p_val'<0.01 {
								local star1="***"
							}
							if `p_val'>=0.01 & `p_val'<0.05 {
								local star1="**"
							}
							if `p_val'>=0.05 & `p_val'<0.1 {
								local star1="*"
							}
							if `p_val'>=0.1 {
								local star1=""
							}
						
						local `v'_b = "``v'_b'"+"`star1'"
						local `v'_se = subinstr("(``v'_se')"," ","",.)
						
						
						
						
						su `v' if e(sample)==1&`v'>=0
						
						local `v'_mean = r(mean)
						local `v'_sd = r(sd)
						local `v'_mean: display %8.3f ``v'_mean'
						local `v'_sd: display %8.3f ``v'_sd'
						local `v'_sd = subinstr("(``v'_sd')"," ","",.)
						
						
						
						local `v'_se = "&"+ "``v'_se'"
						
						local `v'_diff = "``v'_mean'" + "&" + "``v'_b'" + "``v'_se'"
						estadd local `v'_diff "``v'_diff'" : bal_joint1
						estadd local `v'_se "``v'_se'" : bal_joint1
						}

		estout bal_joint1 ///
						using "$tables/balance_postignite_wpred.tex", style(tex) ///
						label nobaselevels collabels(none) mlabels(none) /// 
						drop(D_female age_17_24 age_25_34 age_35_44 age_45_54 age_55_64 age_65plus D_Black prior_offense_1y D_public_defender ///
						D_missing pctebll pc_black pc_hs log_med_hhincome _cons) ///
						cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
						starlevels(* 0.10 ** 0.05 *** 0.01) /// 
						stats(foo D_female_diff age_17_24_diff age_25_34_diff age_35_44_diff age_45_54_diff age_55_64_diff age_65plus_diff ///
							D_Black_diff prior_offense_1y_diff D_public_defender_diff ///
							foo foo pctebll_diff pc_black_diff pc_hs_diff log_med_hhincome_diff D_missing_diff foo  Fs ///
							foo foo return_3m_hat_diff ///
						 foo foo stay_diff foo Num , /// 
						labels("\textbf{Panel A: Individual Characteristics}"  "Female" "Age 18-24" "Age 25-34" "Age 35-44" "Age 45-54" "Age 55-64" "Age 65+" ///
						"Black" "Booked in Past Year" "Has a Public Defender" ///
						" " "\textbf{Panel B: Census Tract Characteristics}" "Share with Elevated Blood Lead Level"  ///
						"Share Black" "Share High School Graduate or Higher" "Log Median Household Income" "Missing Census Tract Information" ///
						"\hspace{0.5cm}" "\emph{F}-Statistic for Joint Test [\emph{p}-value]" ///
						" " "\textbf{Panel C: Predicted Risk}" "Predicted 3-Month Recidivism" ///
						" " "\textbf{Panel D: First Stage}" "Months in Jail" "\hspace{0.5cm}" "Observations")) replace

